package com.kust.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.eduService.entity.excel.SubjectData;
import com.kust.eduService.entity.subject.OneSubject;
import com.kust.eduService.entity.subject.TwoSubject;
import com.kust.eduService.listener.SubjectExcelListener;
import com.kust.eduservice.mapper.EduSubjectMapper;
import com.kust.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.serviceBase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, com.kust.eduservice.entity.Subject> implements EduSubjectService {


    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        //文件输入流
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            throw new GuliException(20001,"获取上传失败");
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //首先先查询所有的一级分类
        QueryWrapper<com.kust.eduservice.entity.Subject> queryWrapperOne = new QueryWrapper<>();
        queryWrapperOne.eq("parent_id",0);
        List<com.kust.eduservice.entity.Subject> eduSubjects1 = baseMapper.selectList(queryWrapperOne);
        //然后查出所有的二级分类
        QueryWrapper<com.kust.eduservice.entity.Subject> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperTwo.ne("parent_id",0);
        List<com.kust.eduservice.entity.Subject> eduSubjects2 = baseMapper.selectList(queryWrapperTwo);
        //创建list集合 用于封装最终数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装1级分类
        for (int i = 0; i < eduSubjects1.size(); i++) {
            com.kust.eduservice.entity.Subject subject1 = eduSubjects1.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject1, oneSubject);
            //封装2级分类
            List<TwoSubject> twoFianlSubjectList = new ArrayList<>();
            for (int j = 0; j < eduSubjects2.size(); j++) {
                com.kust.eduservice.entity.Subject eduSubject2 = eduSubjects2.get(j);
                //判断二级分类parentId复制到twoSubject中
                if(eduSubject2.getParentId().equals(subject1.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    twoFianlSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFianlSubjectList);
            finalSubjectList.add(oneSubject);
        }

        return finalSubjectList;
    }
}
